package com.carleasoft.mps.cms.business.drug.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.cms.business.drug.bean.entity.DrugOilManage;
import com.carleasoft.mps.cms.business.drug.bean.po.DrugOilManagePagePo;
import com.carleasoft.mps.cms.business.drug.dao.DrugOilManageMapper;
import com.carleasoft.mps.cms.business.drug.service.DrugOilManageService;
import com.carleasoft.mps.cms.business.train.bean.entity.TrainConsume;
import com.carleasoft.mps.cms.business.train.bean.entity.TrainConsumeItem;
import com.carleasoft.mps.cms.business.train.constants.ExceptionTrainEnum;
import com.carleasoft.mps.cms.business.train.service.TrainConsumeItemService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @Description:
 * @Author: fan
 */
@Service
public class DrugOilManageServiceImpl extends ServiceImpl<DrugOilManageMapper, DrugOilManage> implements DrugOilManageService {
    @Autowired
    private TrainConsumeItemService trainConsumeItemService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(DrugOilManage drugOilManage) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        drugOilManage.setCode(UUID.randomUUID().toString().replaceAll("-",""));
        drugOilManage.setCreatePersonCode(user.getUserCode());
        drugOilManage.setCreatePersonName(user.getUserName());
        drugOilManage.setCreateTime(LocalDateTime.now());
        drugOilManage.setUpdatePersonCode(user.getUserCode());
        drugOilManage.setUpdatePersonName(user.getUserName());
        drugOilManage.setUpdateTime(LocalDateTime.now());
        return this.save(drugOilManage);
    }

    @Override
    public boolean edit(DrugOilManage drugOilManage) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        drugOilManage.setUpdatePersonCode(user.getUserCode());
        drugOilManage.setUpdatePersonName(user.getUserName());
        drugOilManage.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<DrugOilManage> eq = Wrappers.lambdaQuery(DrugOilManage.class).eq(DrugOilManage::getCode, drugOilManage.getCode());
        return this.update(drugOilManage,eq);
    }

    @Override
    public boolean delete(DrugOilManage drugOilManage) {
        IBeanUtil.paramVerify(drugOilManage,"code");
        LambdaQueryWrapper<DrugOilManage> eq = Wrappers.lambdaQuery(DrugOilManage.class).eq(DrugOilManage::getCode, drugOilManage.getCode());
        int num = trainConsumeItemService.count(new QueryWrapper<TrainConsumeItem>().eq("DRUG_CODE", drugOilManage.getCode()));
        if (num>0){
            throw new BusinessException(ExceptionTrainEnum.DRUG_USED_ERROR);
        }
        return this.remove(eq);
    }

    @Override
    public Page<DrugOilManage> pageQuery(DrugOilManagePagePo drugOilManagePagePo) {
        IBeanUtil.paramVerify(drugOilManagePagePo,"type");
        Page page = new Page(drugOilManagePagePo.getCurrent(),drugOilManagePagePo.getSize());
        QueryWrapper<DrugOilManage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TYPE",drugOilManagePagePo.getType());
        if (StringUtils.isNotBlank(drugOilManagePagePo.getName())){
            queryWrapper.like("NAME",drugOilManagePagePo.getName());
        }
        queryWrapper.orderByDesc("UPDATE_TIME");
        return this.page(page,queryWrapper);
    }

    @Override
    public List<DrugOilManage> queryList(DrugOilManage drugOilManage) {
        IBeanUtil.paramVerify(drugOilManage,"type");
        return this.list(Wrappers.lambdaQuery(DrugOilManage.class).eq(DrugOilManage::getType, drugOilManage.getType()));
    }

    @Override
    public DrugOilManage detail(DrugOilManage drugOilManage) {
        IBeanUtil.paramVerify(drugOilManage,"code");
        LambdaQueryWrapper<DrugOilManage> eq = Wrappers.lambdaQuery(DrugOilManage.class).eq(DrugOilManage::getCode, drugOilManage.getCode());
        return this.getOne(eq);
    }
}
